package com.newcapec.erp.ui;

import com.newcapec.erp.main.DataInit;
import com.newcapec.erp.utils.XMLFReader;
import com.newcapec.erp.utils.XMLFUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class TableUI {
    private static JScrollPane scrollPane;
    private static JPopupMenu m_popupMenu;
    private static List<JTable> jTables = new ArrayList<>();
    public  static int[] currentPage = {1,1,1,1,1,1};
    public static int count;
    //表格展示数据UI
    public static JPanel getTableUI(int type, Vector<Vector<String>> data, int page) {
        JPanel panel = new JPanel();
        panel.setLayout(null);
        // 创建一个TableModel对象
        TableModel dm = new DefaultTableModel();
        // 告诉JTable以什么格式解析数据
        JTable jtable = new JTable(dm);
        jtable.getTableHeader().setReorderingAllowed(false);
        jtable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
        jtable.setRowHeight(35);
        createPopupMenu();

        //判断新增表或更新表
        if(jTables.size() <= type){
            jTables.add(jtable);
        }else {
            jTables.set(type,jtable);
        }

        // 然后为JTable对象添加点击事件
        jtable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(type,evt);
            }
        });

        Vector<String> columnNames = getColumnNames(type);
        //获取需要展示数据的最大页数
        count = data.size();
        int maxPage = count % 15 == 0 ? count/15 : count/15+1;
        Vector<Vector<String>> showData = XMLFReader.getData(data, page, maxPage);
        // 给Jtable设置数据 data就是单元格要实现的数据 columnNames就是表格的列名
        TableModel dataModel = new DefaultTableModel(showData, columnNames);
        jtable.setModel(dataModel);

        if(type == DataInit.TYPE_USER){
            scrollPane = new JScrollPane(jtable);
            scrollPane.setBounds(25, 20, 1000, 58);

            JButton updateBtn = new JButton("修改信息");
            updateBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    DataInit.adminEntity = XMLFReader.getAdmin();
                    new PersonalDataUI();
                }
            });
            updateBtn.setBounds(25,100,100,40);
            panel.add(updateBtn);
        }else {
            // 把jtable放到JScrollPanl里面
            scrollPane = new JScrollPane(jtable);
            scrollPane.setBounds(25, 40, 1000, 600);

            //获取查询区域panel
            JPanel selectPanel = getSelectPanel(type);
            if(selectPanel != null){
                selectPanel.setBounds(25, 0, 1000, 40);
                panel.add(selectPanel);
            }
            //分页操作
            pagesBtn(panel,type,data,maxPage);
        }
        panel.add(scrollPane);
        return panel;
    }

    //定义分页按钮
    public static void pagesBtn(JPanel panel,int type,Vector<Vector<String>> data,int maxPage){
        JLabel jLabel=new JLabel("共有 "+ count +" 条数据 "+"当前 页/总 页数: "+currentPage[type]+"/"+maxPage);
        jLabel.setBounds(150,600,300,30);
        panel.add(jLabel);

        //首页按钮
        JButton jButton1=new JButton("首页");
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int currentType = MainUI.tab.getSelectedIndex();
                currentPage[currentType] = 1;
                //给Jtable设置数据 data就是单元格要实现的数据  columnNames就是表格的列名
                MainUI.updateMainUI(type,data,currentPage[currentType]);
                jLabel.setText("共有 "+ count +" 条数据 "+"当前 页/总 页数: "+currentPage[currentType]+"/"+maxPage);
            }
        });
        jButton1.setBounds(350,600,100,30);
        panel.add(jButton1);

        //上一页按钮
        JButton jButton2=new JButton("上一页");
        jButton2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int currentType = MainUI.tab.getSelectedIndex();
                if(currentPage[currentType] == 1) {
                    JOptionPane.showMessageDialog(null, "已经是第一页数据!", "提示",JOptionPane.ERROR_MESSAGE);
                }else {
                    currentPage[currentType] -= 1;
                    MainUI.updateMainUI(type,data,currentPage[currentType]);
                    jLabel.setText("共有 "+ count +" 条数据 "+"当前 页/总 页数: "+currentPage[currentType]+"/"+maxPage);
                }
            }
        });
        jButton2.setBounds(460,600,100,30);
        panel.add(jButton2);

        //下一页按钮
        JButton jButton3=new JButton("下一页");
        jButton3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int currentType = MainUI.tab.getSelectedIndex();
                if(currentPage[currentType] >= maxPage) {
                    JOptionPane.showMessageDialog(null, "已经是最后一页数据!", "提示",JOptionPane.ERROR_MESSAGE);
                }else {
                    currentPage[currentType] += 1;
                    MainUI.updateMainUI(type,data,currentPage[currentType]);
                    jLabel.setText("共有 "+ count+" 条数据 "+"当前 页/总 页数: "+currentPage[currentType]+"/"+maxPage);
                }
            }

        });
        jButton3.setBounds(570,600,100,30);
        panel.add(jButton3);

        //尾页按钮
        JButton jButton4=new JButton("尾页");
        jButton4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int currentType = MainUI.tab.getSelectedIndex();
                currentPage[currentType] = maxPage;
                MainUI.updateMainUI(type,data,currentPage[currentType]);
                jLabel.setText("共有 "+ count +" 条数据 "+"当前 页/总 页数: "+currentPage[currentType]+"/"+maxPage);
            }
        });
        jButton4.setBounds(680,600,100,30);
        panel.add(jButton4);
    }

    //根据类型获取表字段名
    public static Vector<String> getColumnNames(int type){
        switch (type){
            case DataInit.TYPE_WAREHOUSE:
                return DataInit.warehouseColumnNames;
            case DataInit.TYPE_STORE:
                return DataInit.storeColumnNames;
            case DataInit.TYPE_SYSTEM:
                return DataInit.systemColumnNames;
            case DataInit.TYPE_USER:
                return DataInit.userColumnNames;
            case DataInit.TYPE_CLIENT:
                return DataInit.clientColumnNames;
            case DataInit.TYPE_INVENTORY:
                return DataInit.inventoryColumnNames;
        }
        return null;
    }

    //根据类型获取查询区域UI
    public static JPanel getSelectPanel(int type){
        switch (type){
            case DataInit.TYPE_WAREHOUSE:
                return SelectPanelUI.getWarehouseSelectPanel();
            case DataInit.TYPE_STORE:
                return SelectPanelUI.getStoreSelectPanel();
            case DataInit.TYPE_SYSTEM:
                return SelectPanelUI.getSystemSelectPanel();
            case DataInit.TYPE_CLIENT:
                return SelectPanelUI.getClientSelectPanel();
            case DataInit.TYPE_INVENTORY:
                return SelectPanelUI.getInventorySelectPanel();
        }
        return null;
    }

    // 给表格添加右键菜单
    private static void createPopupMenu() {
        m_popupMenu = new JPopupMenu();
        JMenuItem updateMenItem = new JMenuItem();
        updateMenItem.setText("  修改  ");

        JMenuItem delMenItem = new JMenuItem();
        delMenItem.setText("  删除  ");

        updateMenItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                //拿到当前选中状态的tab页所对应的管理类型
                int currentType = MainUI.tab.getSelectedIndex();
                // 该操作需要做的事
                int index =(currentPage[currentType] -1)*15 + jTables.get(currentType).getSelectedRow();
                XMLFUtils.updateDataByFirstValue(currentType,index,currentPage[currentType]);
            }
        });

        delMenItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                //拿到当前选中状态的tab页所对应的管理类型
                int currentType = MainUI.tab.getSelectedIndex();
                // 该操作需要做的事
                int index =(currentPage[currentType] -1)*15 + jTables.get(currentType).getSelectedRow();
                System.out.println(index);
                int n = JOptionPane.showConfirmDialog(null, "确定删除该条数据吗?", "删除提示", JOptionPane.YES_NO_OPTION); // 返回值为0或1
                if (n==0) {
                    XMLFUtils.deleteDataByFirstValue(currentType,index,currentPage[currentType]);
                }
            }
        });
        m_popupMenu.add(updateMenItem);
        m_popupMenu.add(delMenItem);
    }

    private static void jTable1MouseClicked(int type,java.awt.event.MouseEvent evt) {
        mouseRightButtonClick(type,evt);
    }

    // 鼠标右键点击事件
    private static void mouseRightButtonClick(int type,java.awt.event.MouseEvent evt) {
        // 判断是否为鼠标的BUTTON3按钮，BUTTON3为鼠标右键
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3 && type != DataInit.TYPE_USER) {
            // 通过点击位置找到点击为表格中的行
            int focusedRowIndex = jTables.get(type).rowAtPoint(evt.getPoint());
            if (focusedRowIndex == -1) {
                return;
            }
            // 将表格所选项设为当前右键点击的行
            jTables.get(type).setRowSelectionInterval(focusedRowIndex, focusedRowIndex);
            // 弹出菜单
            //m_popupMenu.show(jtable, evt.getX(), evt.getY());
            m_popupMenu.show(jTables.get(type), evt.getX(), evt.getY());
        }
    }
}
